﻿Public Class frmMain
    Public Authentication As Boolean = False
    Public frmQuanLyKhachHang_Open As Boolean = False
    Public frmHopDong_Open As Boolean = False
    Public UserName As String = ""
    Public UserId As Integer
    Shared Sub Main()
        ' Starts the application.
        Application.Run(New frmMain)
    End Sub
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Me.WindowState = FormWindowState.Maximized
            Me.MaximizeBox = True
            'Load Tab Default
            MenuHeThong.Select()
            'Login System -Check Account Info
            ShowChildForm(frmLogin, False, False)
        Catch ex As Exception
            ShowError(ex)
        End Try
    End Sub
    Private Sub DangNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ShowChildForm(New frmLogin, False, False)
    End Sub
    Public Sub ShowChildForm(ByVal Frm As System.Windows.Forms.Form, Optional ByVal MinimumBox As Boolean = True, Optional ByVal MaximumBox As Boolean = True)
        Try
            Dim forms() As Form = Me.MdiChildren
            If forms.Length > 0 Then
                Dim ChildForm As New Form
                For Each ChildForm In Me.MdiChildren
                    If ChildForm.Name = Frm.Name Then
                        ChildForm.Activate()
                        Exit Sub
                    Else
                        Frm.MdiParent = frmMain.ActiveForm
                        Frm.MaximizeBox = MaximumBox
                        Frm.MinimizeBox = MinimumBox
                        CenterForm(Frm)
                        Frm.Show()
                    End If
                Next
            Else
                Frm.MdiParent = frmMain.ActiveForm
                Frm.MaximizeBox = MaximumBox
                Frm.MinimizeBox = MinimumBox
                CenterForm(Frm)
                Frm.Show()
            End If
        Catch ex As Exception

        End Try
    End Sub
    Public Sub CenterForm(ByVal Frm As Form)
        Frm.StartPosition = FormStartPosition.CenterScreen
    End Sub
    Private Sub MenuKhuVuc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuKhuVuc.Click
        Try
            ShowChildForm(frmTinhThanh, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuKhachHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuKhachHang.Click
        Try
            ShowChildForm(QuanLyKhachHang.frmDanhMucKhachHang, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuBanHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuBanHang.Click
        Try
            ShowChildForm(QuanLyKhachHang.frmHopDong, False, False)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
            'ShowError()
        End Try
    End Sub

    Private Sub MenuNhanVienTiepThi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuNhanVienTiepThi.Click
        Try
            ShowChildForm(frmNhanVienTiepThi, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#Region "Menu Kho"
    Private Sub MenuDonViTinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDonViTinh.Click
        Try
            ShowChildForm(frmDVT, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub MenuLoaiSanPham_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuLoaiSanPham.Click
        Try
            ShowChildForm(frmLoaiSanPham, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub


    Private Sub MenuSanPham_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuSanPham.Click
        Try
            ShowChildForm(frmSanPham, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuNhapKho_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuNhapKho.Click
        Try
            ShowChildForm(frmPhieuNhapKho, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuHangTraVe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuHangTraVe.Click
        Try
            ShowChildForm(frmHangTraVe, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuKho_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuKho.Click
        Try
            ShowChildForm(frmQuanLyKho, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region

    Private Sub MenuBangGiaSanPham_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuBangGiaSanPham.Click
        Try
            ShowChildForm(frmBangGia, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuMucTruHoaDon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuMucTruHoaDon.Click
        Try
            ShowChildForm(frmMucTruHoaDon, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuNhomSuDung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuNhomSuDung.Click
        Try
            ShowChildForm(frmGroup, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuNguoiSuDung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuNguoiSuDung.Click
        Try
            ShowChildForm(frmUsers, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuSaoLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuSaoLuu.Click
        Try
            ShowChildForm(frmBackup, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuPhuchoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuPhuchoi.Click
        Try
            ShowChildForm(frmRestore, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuKetThuc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuKetThuc.Click
        Try
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Vui lòng kiểm tra dữ liệu bạn đang làm việc trước khi đăng xuất." & vbCrLf & _
                  "Bạn đồng ý thoát khỏi chương trình?", _
                  "Đăng xuất chương trình", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Question)
            If dlgRes = DialogResult.OK Then
                Application.Exit()
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuDangXuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDangXuat.Click
        Try
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Vui lòng kiểm tra dữ liệu bạn đang làm việc trước khi đăng xuất." & vbCrLf & _
                  "Tất cả các cửa sổ bạn đang làm việc sẽ được đóng lại." & vbCrLf & _
                  "Bạn đồng ý đăng xuất tài khoản?", _
                  "Đăng xuất tài khoản", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Question)
            If dlgRes = DialogResult.OK Then
                UserName = ""
                lblUser.Text = ""
                Authentication = False
                'Close All ChildForm
                For Each ChildForm In Form.ActiveForm.MdiChildren()
                    ChildForm.Close()
                    'If ChildForm.Name = Frm.Name Then
                    '    ChildForm.Activate()
                    '    Exit Sub
                    'End If
                Next
                ShowChildForm(frmLogin, False, False)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuPhanQuyen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuPhanQuyen.Click
        Try
            ShowChildForm(frmPhanQuyen, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuThongKeNhapKho_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuThongKeNhapKho.Click
        Try
            ShowChildForm(frmThongKeNhapKho, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuDoanhSoKhachHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDoanhSoKhachHang.Click
        Try
            ShowChildForm(QuanLyKhachHang.frmThongKeDoanhThu, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuSanLuongTieuThu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuSanLuongTieuThu.Click
        Try
            ShowChildForm(QuanLyKhachHang.frmThongKeSanLuongTieuThu, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuThongKeXuatKho1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuThongKeXuatKho1.Click
        Try
            ShowChildForm(QuanLyKhachHang.frmThongKeXuatKho, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuThongKeNhapKho1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuThongKeNhapKho1.Click
        Try
            ShowChildForm(frmThongKeNhapKho, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuThongKeXuatKho_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuThongKeXuatKho.Click
        Try
            ShowChildForm(QuanLyKhachHang.frmThongKeXuatKho, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuThongKeSoLuongTon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuThongKeSoLuongTon.Click
        Try
            ShowChildForm(frmThongKeSoLuongTon, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuThongKeSoLuongTon1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuThongKeSoLuongTon1.Click
        Try
            ShowChildForm(frmThongKeSoLuongTon, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuDoiMatKhau_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDoiMatKhau.Click
        Try
            ShowChildForm(frmChangePassword, False, False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub ButtonItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem1.Click
        Try
            Dim CurrentDir As String = IO.Path.GetDirectoryName(Application.ExecutablePath)
            Dim PathHelpFile As String = CurrentDir + "\HuongDanSuDung.doc"
            Dim objWord As New Microsoft.Office.Interop.Word.Application
            objWord.Visible = True
            objWord.Documents.Open(PathHelpFile, , False, True)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    
    Private Sub MenuSanPhamNhieuGia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuSanPhamNhieuGia.Click
        Try
            ShowChildForm(frmGiaSanPhamTheoKhachHang, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ButtonItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuGiamTruRieng.Click
        Try
            ShowChildForm(frmGiamTruTheoKhachHang, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuPrinter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuPrinter.Click
        Try
            ShowChildForm(ConfigPrinter, False, False)
        Catch ex As Exception

        End Try
    End Sub

    
    Private Sub MenuPhiVanChuyen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuPhiVanChuyen.Click
        Try
            ShowChildForm(frmThongKePhiVanChuyen, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuNhomGia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuNhomGia.Click
        Try
            ShowChildForm(frmGiaTheoNhom, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuXuatKhoNoiBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuXuatKhoNoiBo.Click
        Try
            ShowChildForm(frmPhieuXuatKhoNoiBo, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuDanhMucKho_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            ShowChildForm(frmPhieuKho, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuKhoXuLy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuKhoXuLy.Click
        Try
            ShowChildForm(frmQuanLyKhoXuLy, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuTheoDoiTonKho_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuTheoDoiTonKho.Click
        Try
            ShowChildForm(frmTheoDoiTon, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuThongKeXuatKhoNoiBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuThongKeXuatKhoNoiBo.Click
        Try
            ShowChildForm(frmThongKeXuatKhoNoiBo, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuTheoDoiDonHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuTheoDoiDonHang.Click
        Try
            ShowChildForm(frmTheoDoiDonHang, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuQuanLyBanHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuQuanLyBanHang.Click

    End Sub
End Class
